package eu.testing.system.controllers;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import eu.testing.system.models.AssignedTest;
import eu.testing.system.models.Result;
import eu.testing.system.models.Test;
import eu.testing.system.models.UserData;

@Controller
public class UserController {
	SystemController controller = new SystemController();

	@RequestMapping(value = "/createNewUser", method = RequestMethod.GET)
	public String createNewUser(ModelMap model) {
		model = controller.addUserData(model);
		return "createNewUser";
	}

	@RequestMapping(value = "/assignTestToUser", method = RequestMethod.GET)
	public String assignTestToUser(ModelMap model) throws SQLException {
		model = controller.addUserData(model);
		List<UserData> users = controller.userDataDao.getAllUsers();
		model.addAttribute("users", users);
		List<Test> tests = controller.testDao.getAllTests();
		model.addAttribute("tests", tests);

		return "assignTestToUser";
	}

	@RequestMapping(value = "/assignTestToUser", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public String assignTestToUser(@RequestBody() String json, ModelMap model)
			throws SQLException, UnsupportedEncodingException {
		model = controller.addUserData(model);

		Gson gson = new GsonBuilder().create();
		AssignedTest assigned = gson.fromJson(json,
				AssignedTest.class);
		controller.assignedDao.insertAssigned(assigned);
		model.addAttribute("status", "Tests successfully assgined!");
		return "testCreationSubmit";

	}

	@RequestMapping(value = "/createNewUser", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public String submitTest(@RequestBody() String json, ModelMap model)
			throws SQLException, UnsupportedEncodingException {
		model = controller.addUserData(model);
		UserData user = (UserData) controller.getObjectFromJson(json,
				UserData.class);
		try {
			controller.userDataDao.insertUser(user);
			model.addAttribute("status", "user '" + (user.getUsername())
					+ "' successfully created!");
		} catch (NullPointerException e) {
			model.addAttribute("error", "User '" + user.getUsername()
					+ "' already exists in the system! Try different username.");
			return "createNewUser";
		}
		return "testCreationSubmit";

	}

	@RequestMapping(value = "/manageUsers", method = RequestMethod.GET)
	public String manageUsers(ModelMap model) {
		model = controller.addUserData(model);
		List<UserData> users = controller.userDataDao.getAllUsers();
		model.addAttribute("users", users);
		return "manageUsers";
	}

	@RequestMapping(value = "/seeSingleUserResult", method = RequestMethod.GET)
	public String seeSingleUserResult(
			@RequestParam(value = "action") String userName, ModelMap model)
			throws IOException, SQLException {
		List<Result> results = controller.resultDao
				.getAllResults(userName);
		model = controller.addUserData(model);
		model.addAttribute("results", results);
		return "seeSingleUserResult";
	}

	@RequestMapping(value = "/seeUserResults", method = RequestMethod.GET)
	public String seeUserResults(ModelMap model) throws IOException,
			SQLException {
		model = controller.addUserData(model);
		if (controller.isAdmin()) {
			List<UserData> users = controller.userDataDao.getAllUsers();
			model.addAttribute("users", users);
		} else {
			return seeSingleUserResult(controller.getUsername(), model);
		}
		return "seeUsersResults";
	}

	@RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
	public String deleteUser(@RequestParam(value = "action") String username,
			ModelMap model) {
		model = controller.addUserData(model);
		controller.userDataDao.deleteUser(username);
		return manageUsers(model);
	}

	@RequestMapping(value = "/editUser", method = RequestMethod.GET)
	public String editUser(@RequestParam(value = "action") String username,
			ModelMap model) {
		model = controller.addUserData(model);
		UserData user = controller.userDataDao.getUser(username);
		model.addAttribute("user", user);
		return "editUser";
	}

	@RequestMapping(value = "/editUser", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public String receiveEditedUser(@RequestBody() String json, ModelMap model)
			throws SQLException, UnsupportedEncodingException {
		model = controller.addUserData(model);
		UserData user = (UserData) controller.getObjectFromJson(json,
				UserData.class);
		controller.userDataDao.editUser(user);
		model.addAttribute("status", "User '" + (user.getUsername())
				+ "' successfully edited!");
		return "testCreationSubmit";

	}

}